<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\bill;

use think\facade\Db;
use app\traits\CategoryRepository;
use app\common\dao\bill\BillCateDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class BillCateRepository
 *
 * @mixin BillCateDao
 */
class BillCateRepository extends BaseRepository
{
    use CategoryRepository;

    const IS_CLASS = [
        1 => '基本费用',
        2 => '能源费用',
        3 => '其它费用',
        4 => '优惠费用',
    ];

    const IS_BASIC = [
        1 => '定金',
        2 => '订金',
        3 => '押金',
        4 => '租金',
    ];

    const IS_ENERGY = [
        11 => '电费',
        12 => '冷水费',
        13 => '热水费',
        14 => '燃气费',
    ];

    const IS_OTHER = [
        21 => '滞纳金',
        22 => '物业费',
    ];

    public function __construct(BillCateDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->select()->toArray();
        foreach ($list as $k => &$v) {
            $v['pid'] = $v['pid'] === 0 ? -1 : $v['pid'];
            $v['class_name'] = self::IS_CLASS[$v['is_class']] ?? '';
            if ($v['is_class'] == 1) {
                $v['set_finance'] = self::IS_BASIC[$v['set_finance']] ?? '--';
            } elseif ($v['is_class'] == 2) {
                $v['set_finance'] = self::IS_ENERGY[$v['set_finance']] ?? '--';
            } elseif ($v['is_class'] == 3) {
                $v['set_finance'] = self::IS_OTHER[$v['set_finance']] ?? '--';
            } else {
                $v['set_finance'] = '--';
            }
        }
        return compact('list', 'count');
    }


    public function addInfo(array $data, int $propertyId = 1, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $data['property_id'] = $propertyId;
            $info = $this->create($data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($propertyId, $userId, 'BILL.CATE.ADD', $data);
            return $info;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            $res = $this->update($info['id'], $data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'BILL.CATE.EDIT', $data);
            return $res;
        });
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'BILL.CATE.DEL', $info);
        return $this->dao->delete($info['id']);
    }


    public function getDetail(int $id)
    {
        return $this->dao->getSearch(['id' => $id])
            ->find();
    }

    public function cateNameExists($cateName, $propertyId = null)
    {
        $where = [
            'cate_name' => $cateName
        ];
        if ($propertyId !== null) {
            $where['property_id'] = $propertyId;
        }
        return $this->dao->existsWhere($where);
    }

    public function getCascaderData(int $propertyId, array $where = [])
    {
        $list = $this->dao->search($where, $propertyId)
            ->field('cate_name,id,pid')
            ->select()->toArray();
        $list = convert_arr_key($list, 'id');
        $res = formatCascaderData($list, 'cate_name');
        return $res;
    }

    public function getTopCateData(int $propertyId, array $where = [])
    {
        $where['level'] = 1;
        $list = $this->dao->search($where, $propertyId)
            ->field('cate_name,id,pid')
            ->select()->toArray();
        return $list;
    }

    /**
     * 根据条件查单个分类 ID
     * @param int $propertyId
     * @param array $where
     * @return mixed
     *
     * @author Amos
     * 2024-06-14 10:07
     */
    public function getBillCateId(int $propertyId, array $where = [])
    {
        return $this->dao->search($where, $propertyId)->value('id');
    }

    /**
     *  根据条件查所有分类 ID
     * @param int $propertyId
     * @param $isClass
     * @return array
     *
     * @author Amos
     * 2024-06-14 10:06
     */
    public function getBillCateIds(int $propertyId, $isClass)
    {
        return $this->dao->getSearch([])
            ->where('property_id', $propertyId)
            ->where('is_class', $isClass)
            ->column('id');
    }
}